package com.xh.bussiness.xkw.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xh.bussiness.tiku.model.BookChapter;
import com.xh.bussiness.tiku.model.BookVersion;
import com.xh.bussiness.tiku.service.IBookChapterService;
import com.xh.bussiness.tiku.service.IBookVersionService;
import com.xh.bussiness.xkw.constant.XkwAPIURL;
import com.xh.bussiness.xkw.dao.XkwChapterMapper;
import com.xh.bussiness.xkw.model.XkwChapter;
import com.xh.bussiness.xkw.model.XkwVersionBook;
import com.xh.bussiness.xkw.service.IXkwChapterService;
import com.xh.bussiness.xkw.service.IXkwVersionBookService;
import com.xh.bussiness.xkw.utils.ConfigUtils;
import com.xh.bussiness.xkw.utils.XkwHttpsUtils;
import com.xh.core.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class XkwChapterServiceImpl extends BaseServiceImpl<XkwChapterMapper, XkwChapter> implements IXkwChapterService {

    private final String XkwApiSeriveUrl;

    @Autowired
    public XkwChapterServiceImpl(ConfigUtils configUtils) {
        XkwApiSeriveUrl = configUtils.getXkwApiSeriveUrl();
    }

    @Autowired
    private ConfigUtils configUtils;

    @Autowired
    private IXkwVersionBookService xkwVersionBookService;
    @Autowired
    private IBookVersionService bookVersionService;
    @Autowired
    private IBookChapterService bookChapterService;
    /**
     *  保存所有学科网章节树
     * @throws Exception
     */
    public void sysXkwChapters() throws Exception {
        List<BookVersion> bookVersions = bookVersionService.queryAll();
        if(!bookVersions.isEmpty()){
            bookVersions.forEach(versionBook->{
                try {
                    List<XkwChapter> xkwChapters = queryXkwChapters(Integer.valueOf(versionBook.getId()));
                    if (!xkwChapters.isEmpty()) {
                        for (XkwChapter xkwChapter : xkwChapters) {
                            BookChapter bookChapter = new BookChapter();
                            bookChapter.setId(String.valueOf(xkwChapter.getXkwId()));
                            bookChapter.setName(xkwChapter.getName());
                            bookChapter.setBookVersionId(versionBook.getId());
                            bookChapter.setParentId(String.valueOf(xkwChapter.getParentId()));
                            bookChapter.setPath(xkwChapter.getPath());
                            bookChapter.setPathName(xkwChapter.getPathName());
                            bookChapter.setParentPathId(xkwChapter.getParentPathId());
                            bookChapter.setParentPathName(xkwChapter.getParentPathName());
                            bookChapterService.insert(bookChapter);
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });
        }
    }

    /**
     *  查询所有学科网章节树
     * @param bookId  课本Id
     * @throws Exception
     */
    public  List<XkwChapter>  queryXkwChapters(Integer bookId) throws Exception {
        List<XkwChapter> xkwChildList = new ArrayList<>();
        String queryUrl = String.format(XkwAPIURL.GET_CHAPTERS_BY_ID,bookId);
        String url = XkwApiSeriveUrl + queryUrl;
        String childCategoriesJson = XkwHttpsUtils.doGet(url);
        if(null == childCategoriesJson){
            return xkwChildList;
        }
        Map childMap = JSONObject.parseObject(childCategoriesJson, Map.class);
        Integer id = Integer.valueOf(childMap.get("id").toString());
        String name = childMap.get("name").toString();
        Integer show = Integer.valueOf(childMap.get("show").toString());
        String childNodeJson = childMap.get("categories").toString();

        XkwChapter xkwChapter = new XkwChapter(id,bookId, name, 0, show);
        xkwChildList.add(xkwChapter);

        getChildChapters(id, bookId, xkwChildList, childNodeJson,"#"+id+"#","#"+name+"#");
        return xkwChildList;
    }


    /**
     * 递归获取子章节
     * @param rootId  父级ID
     * @param bookId   课本ID
     * @param xkwChapterList  存储集合
     * @param childNodeJson   子节点json
     */
    private void getChildChapters(Integer rootId,Integer bookId, List<XkwChapter> xkwChapterList, String childNodeJson, String parentPathId, String parentPathName) {
        List<Map> parse = (List<Map>) JSONArray.parse(childNodeJson);
        if(!parse.isEmpty()){
            parse.forEach(map -> {
                Integer id = Integer.valueOf(map.get("id").toString());
                String name = map.get("name").toString();
                Integer show = Integer.valueOf(map.get("show").toString());
                if(show == 1){
                    XkwChapter xkwCategory = new XkwChapter(id,bookId, name, rootId, show);
                    xkwCategory.setParentPathId(parentPathId);
                    xkwCategory.setParentPathName(parentPathName);
                    xkwCategory.setPath(xkwCategory.getParentPathId()+id+"#");
                    xkwCategory.setPathName(xkwCategory.getParentPathName()+name+"#");
                    xkwChapterList.add(xkwCategory);

                    String childJson = map.get("categories").toString();
                    getChildChapters(id,bookId,xkwChapterList,childJson, xkwCategory.getPath(), xkwCategory.getPathName());
                }
            });
        }
    }


}
